<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资金所属方id" prop="fundOwnerId">
        <el-select v-model="queryParams.fundOwnerId" placeholder="请选择资金所属方id" clearable>
          <el-option
            v-for="dict in dict.type.whole_fund_owner_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资金所属方名称" prop="fundOwnerName">
        <el-select v-model="queryParams.fundOwnerName" placeholder="请选择资金所属方名称" clearable>
          <el-option
            v-for="dict in dict.type.whole_fund_owner_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资金所属方类型" prop="fundOwnerType">
        <el-select v-model="queryParams.fundOwnerType" placeholder="请选择资金所属方类型" clearable>
          <el-option
            v-for="dict in dict.type.whole_fund_owner_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="消费金额" prop="consumerFund">
        <el-input
          v-model="queryParams.consumerFund"
          placeholder="请输入消费金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值金额" prop="rechargeAmount">
        <el-input
          v-model="queryParams.rechargeAmount"
          placeholder="请输入充值金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费人id" prop="consumerId">
        <el-input
          v-model="queryParams.consumerId"
          placeholder="请输入消费人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费人名" prop="consumerName">
        <el-input
          v-model="queryParams.consumerName"
          placeholder="请输入消费人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费时间" prop="consumerTime">
        <el-date-picker clearable
          v-model="queryParams.consumerTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择消费时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="消费用途" prop="consumerPlace">
        <el-select v-model="queryParams.consumerPlace" placeholder="请选择消费用途" clearable>
          <el-option
            v-for="dict in dict.type.whole_fund_consumer_place"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['whole:fundRecord:add']"
          v-if="this.$store.state.user.name.charAt(0)=='C'"
        >新增</el-button>
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['whole:fundRecord:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['whole:fundRecord:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['whole:fundRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fundRecordList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="资金所属方id" align="center" prop="fundOwnerId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_fund_owner_id" :value="scope.row.fundOwnerId"/>
        </template>
      </el-table-column>
      <el-table-column label="资金所属方名称" align="center" prop="fundOwnerName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_fund_owner_name" :value="scope.row.fundOwnerName"/>
        </template>
      </el-table-column>
      <el-table-column label="资金所属方类型" align="center" prop="fundOwnerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_fund_owner_type" :value="scope.row.fundOwnerType"/>
        </template>
      </el-table-column>
      <el-table-column label="消费金额" align="center" prop="consumerFund" />
      <el-table-column label="充值金额" align="center" prop="rechargeAmount" />
      <el-table-column label="消费人id" align="center" prop="consumerId" />
      <el-table-column label="消费人名称" align="center" prop="consumerName" />
      <el-table-column label="消费时间" align="center" prop="consumerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consumerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费用途" align="center" prop="consumerPlace">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_fund_consumer_place" :value="scope.row.consumerPlace"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['whole:fundRecord:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['whole:fundRecord:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资金记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资金所属方id" prop="fundOwnerId">
          <el-select v-model="form.fundOwnerId" placeholder="请选择资金所属方id">
            <el-option
              v-for="dict in dict.type.whole_fund_owner_id"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资金所属方名称" prop="fundOwnerName">
          <el-select v-model="form.fundOwnerName" placeholder="请选择资金所属方名称">
            <el-option
              v-for="dict in dict.type.whole_fund_owner_name"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资金所属方类型" prop="fundOwnerType">
          <el-select v-model="form.fundOwnerType" placeholder="请选择资金所属方类型">
            <el-option
              v-for="dict in dict.type.whole_fund_owner_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消费金额" prop="consumerFund">
          <el-input v-model="form.consumerFund" placeholder="请输入消费金额" />
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="消费用途" prop="consumerPlace">
          <el-select v-model="form.consumerPlace" placeholder="请选择消费用途">
            <el-option
              v-for="dict in dict.type.whole_fund_consumer_place"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFundRecord, getFundRecord, delFundRecord, addFundRecord, updateFundRecord } from "@/api/whole/fundRecord";

export default {
  name: "FundRecord",
  dicts: ['whole_fund_owner_type', 'whole_fund_owner_id', 'whole_fund_owner_name', 'whole_fund_consumer_place'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资金记录表格数据
      fundRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fundOwnerId: null,
        fundOwnerName: null,
        fundOwnerType: null,
        consumerFund: null,
        rechargeAmount: null,
        consumerId: null,
        consumerName: null,
        consumerTime: null,
        consumerPlace: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fundOwnerId: [
          { required: true, message: "资金所属方id不能为空", trigger: "change" }
        ],
        fundOwnerName: [
          { required: true, message: "资金所属方名称不能为空", trigger: "change" }
        ],
        fundOwnerType: [
          { required: true, message: "资金所属方类型不能为空", trigger: "change" }
        ],
        consumerFund: [
          { required: true, message: "消费金额不能为空", trigger: "blur" }
        ],
        rechargeAmount: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
        consumerPlace: [
          { required: true, message: "消费用途不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资金记录列表 */
    getList() {
      this.loading = true;
      listFundRecord(this.queryParams).then(response => {
        this.fundRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fundOwnerId: null,
        fundOwnerName: null,
        fundOwnerType: null,
        consumerFund: null,
        rechargeAmount: null,
        consumerId: null,
        consumerName: null,
        consumerTime: null,
        consumerPlace: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资金记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFundRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资金记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFundRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFundRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资金记录编号为"' + ids + '"的数据项？').then(function() {
        return delFundRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('whole/fundRecord/export', {
        ...this.queryParams
      }, `fundRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
